﻿using System.Text.Json.Serialization;

namespace Journeo_backend.Models.GPT
{
    public class Schema
    {
        [JsonPropertyName("type")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public string? Type { get; set; }

        [JsonPropertyName("properties")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public Dictionary<string, SchemaProperty>? Properties { get; set; }

        [JsonPropertyName("required")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public List<string>? Required { get; set; }

        [JsonPropertyName("additionalProperties")]
        public bool AdditionalProperties { get; set; }

        [JsonPropertyName("items")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public Schema? Items { get; set; }
    }

    public class SchemaProperty
    {
        [JsonPropertyName("type")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public string? Type { get; set; }

        [JsonPropertyName("items")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public Schema? Items { get; set; }

        [JsonPropertyName("properties")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public Dictionary<string, SchemaProperty>? Properties { get; set; }

        [JsonPropertyName("required")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public List<string>? Required { get; set; }

        [JsonPropertyName("additionalProperties")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public bool? AdditionalProperties { get; set; }
    }


    // 预定义Schema
    public static class SchemaConstants
    {
        // 简单对话
        public static Schema SimpleResponse => new Schema
        {
            Type = "object",
            Properties = new Dictionary<string, SchemaProperty>
            {
                ["response"] = new SchemaProperty
                {
                    Type = "string"
                }
            },
            Required = new List<string> { "response" },
            AdditionalProperties = false
        };

        // 数学推理
        public static Schema MathReasoning => new Schema
        {
            Type = "object",
            Properties = new Dictionary<string, SchemaProperty>
            {
                ["steps"] = new SchemaProperty
                {
                    Type = "array",
                    Items = new Schema
                    {
                        Type = "object",
                        Properties = new Dictionary<string, SchemaProperty>
                        {
                            ["explanation"] = new SchemaProperty { Type = "string" },
                            ["output"] = new SchemaProperty { Type = "string" }
                        },
                        Required = new List<string> { "explanation", "output" },
                        AdditionalProperties = false
                    }
                },
                ["final_answer"] = new SchemaProperty { Type = "string" }
            },
            Required = new List<string> { "steps", "final_answer" },
            AdditionalProperties = false
        };

        // 旅游推荐
        public static Schema TravelRecommendation => new Schema
        {
            Type = "object",
            Properties = new Dictionary<string, SchemaProperty>
            {
                ["analysis"] = new SchemaProperty
                {
                    Type = "object",
                    Properties = new Dictionary<string, SchemaProperty>
                    {
                        ["summary"] = new SchemaProperty { Type = "string" },
                        ["travel_tips"] = new SchemaProperty { Type = "string" },
                        ["budget_analysis"] = new SchemaProperty { Type = "string" }
                    },
                    Required = new List<string> { "summary", "travel_tips", "budget_analysis" },
                    AdditionalProperties = false
                },
                ["recommended_spots"] = new SchemaProperty
                {
                    Type = "array",
                    Items = new Schema
                    {
                        Type = "object",
                        Properties = new Dictionary<string, SchemaProperty>
                        {
                            ["name"] = new SchemaProperty { Type = "string" },
                            ["description"] = new SchemaProperty { Type = "string" },
                        },
                        Required = new List<string> { "name", "description"},
                        AdditionalProperties = false
                    }
                }
            },
            Required = new List<string> { "analysis", "recommended_spots" },
            AdditionalProperties = false
        };

        // 景点分析
        public static Schema SpotAnalysis => new Schema
        {
            Type = "object",
            Properties = new Dictionary<string, SchemaProperty>
            {
                ["spot_details"] = new SchemaProperty
                {
                    Type = "array",
                    Items = new Schema
                    {
                        Type = "object",
                        Properties = new Dictionary<string, SchemaProperty>
                        {
                            ["name"] = new SchemaProperty { Type = "string" },
                            ["description"] = new SchemaProperty { Type = "string" },
                            ["detailed_analysis"] = new SchemaProperty { Type = "string" },
                            ["visiting_tips"] = new SchemaProperty { Type = "string" },
                            ["budget_estimation"] = new SchemaProperty { Type = "string" },
                            ["recommended_duration"] = new SchemaProperty { Type = "string" }
                        },
                        Required = new List<string> { "name", "description", "detailed_analysis", "visiting_tips", "budget_estimation", "recommended_duration" },
                        AdditionalProperties = false
                    }
                },
                ["analysis"] = new SchemaProperty
                {
                    Type = "object",
                    Properties = new Dictionary<string, SchemaProperty>
                    {
                        ["summary"] = new SchemaProperty { Type = "string" },
                        ["travel_tips"] = new SchemaProperty { Type = "string" },
                        ["budget_analysis"] = new SchemaProperty { Type = "string" }
                    },
                    Required = new List<string> { "summary", "travel_tips", "budget_analysis" },
                    AdditionalProperties = false
                }
            },
            Required = new List<string> {"spot_details", "analysis" },
            AdditionalProperties = false
        };

        // 连续聊天响应
        public static Schema ContinuousChatResponse => new Schema
        {
            Type = "object",
            Properties = new Dictionary<string, SchemaProperty>
            {
                ["response"] = new SchemaProperty { Type = "string" },
                ["updated_history"] = new SchemaProperty
                {
                    Type = "array",
                    Items = new Schema
                    {
                        Type = "object",
                        Properties = new Dictionary<string, SchemaProperty>
                        {
                            ["role"] = new SchemaProperty { Type = "string" },
                            ["content"] = new SchemaProperty { Type = "string" }
                        },
                        Required = new List<string> { "role", "content" },
                        AdditionalProperties = false
                    }
                }
            },
            Required = new List<string> { "response", "updated_history" },
            AdditionalProperties = false
        };
    }
}
